type Student = { name: string, score: number };
type Teacher = { name: string, age: number, subject: string };

type User = Student & Teacher;

const user: User = {
  name: "John",
  score: 100,
  age: 30,
  subject: "Math"
}

// 交叉类型，不能直接使用简单字面量类型和基础类型，得到的是never
// type Width = number & string;
// type Color = "red" & "green" & "blue";

type P = {
  name: string,
  sex: string
}

type T = {
  name: string | number | undefined,
  age: number
}

type PT = P & T;

const pt: PT = {
  name: "John",
  sex: "男",
  age: 20
}

// 交叉类型和联合类型一起使用
type Params = string | number | boolean;
type A = Params & string;

// 使用交叉类型实现继承的效果
type Goods = {
  id: number,
  name: string,
  price: number
}

type Cart = Goods & { count: number };

type Order = Goods & {
  count: number,
  totalPrice: number
}

let goods: Goods = {
  id: 1,
  name: "apple",
  price: 5
}

let cart: Cart = {
  id: 1,
  name: "apple",
  price: 5,
  count: 2
}

let order: Order = {
  id: 1,
  name: "apple",
  price: 5,
  count: 2,
  totalPrice: 10
}

goods = cart;
// cart = goods; // 报错